package forumSystem;

import java.util.Vector;

import msgParser.Strings;

//import protocol.Notification;
import forumSystem.Forum;
import forumSystem.ForumNotifications;
import forumSystem.ForumSystemImpl;
//import forumSystem.Policy;
import forumSystem.Post;
import forumSystem.Reply;

public class NotificationBuilder {

	private static boolean push = true;
	private static boolean pull = false;
	
	public static void buildNotification(int id, String kind ) 
	{
		String message;
		Vector<Integer> toNotify = new Vector<Integer>();
		Forum f;
		int userID = 0;
		int author = 0;
		
		if(kind.equals("edit_reply") || kind.equals("addReply"))
		{
			userID = ForumSystemImpl.getReplyByID(id).getUserID();
			author = ForumSystemImpl.getPostByReplyID(id).getUserID();
			f = ForumSystemImpl.getForumByReplyId(id);
		}
		else if(kind.equals("edit_post"))
		{
			userID = ForumSystemImpl.getPostByID(id).getUserID();
			f = ForumSystemImpl.getForumByPostId(id);
		}
		else
			f = null;
		
		if( f != null)
		{
			Notification n;
			int fid = f.getId();
			
			Vector<String> info = getInfo(kind, id);
			message = buildMessage(info);
			
			int policy_id = f.getPolictyID(); 
			System.out.println(policy_id);
			ForumPolicy policy = ForumSystemImpl.getPolicyByID(policy_id);
			System.out.println(policy==null);
			
			if((policy.getWhoNotify() & Strings.FORUM_REPLIES_USERS_NOTIFY) != 0) // notify repliers only
			{
				int postID = ForumSystemImpl.getPostByReplyID(id).getId();
				toNotify = ForumSystemImpl.getRepliersOfPost(postID);
				toNotify.add(author);
				toNotify.removeElement(userID);
				toNotify.removeElement(userID);
			}
			else if((policy.getWhoNotify() & Strings.FORUM_MEMBERS_NOTIFY) != 0) // notify all forum users
			{				
				toNotify = ForumSystemImpl.getForumUsers(fid);
				toNotify.removeElement(userID);
				toNotify.removeElement(userID);
			}
			else
			{
				System.out.println("WTFFFFFFFFFFFFFFF" + policy.getWhoNotify());
			}
			
			n = new Notification(message, toNotify);
			if(policy.getWhenNotify() == Strings.IMMEDIATE_NOTIFY)
				ForumNotifications.addNotification(fid, n, push);
			else
				ForumNotifications.addNotification(fid, n, pull);
			
		}
		else
		{
			System.out.println("forum is null");
		}
	}
	
	
	private static String buildMessage(Vector<String> params)
	{
		String message;
		
		String kind = params.elementAt(0);		
		String editor = params.elementAt(1);
		String title = params.elementAt(2);
		
		if(kind.equals("edit_reply"))
			message = "User " + editor + " edited his reply in post: " + title ;
		else if(kind.equals("edit_post"))
			message = "User " + editor + " edited his post: " + title ;
		else if(kind.equals("addReply"))
			message = "User " + editor + " added a reply\n" + "" + "to the post: " + title + ".";
		else
			message = "there is a notification for you";
		
		return message;
	}
	
	private static Vector<String> getInfo(String kind, int id)
	{
		Vector<String> info = new Vector<String>();
		String editor;
		int editorID;
		
		if(kind.equals("edit_reply") || kind.equals("addReply"))
		{
			
			Reply rep = ForumSystemImpl.getReplyByID(id);
			editorID = rep.getUserID();
			editor = ForumSystemImpl.getUserByID(editorID).getUsername();
			
			Post p = ForumSystemImpl.getPostByReplyID(id);
			String Title = p.getTitle();
			
			info.add(kind);
			info.add(editor);
			info.add(Title);
		}
		
		else if(kind.equals("edit_post"))
		{
			
			Post p = ForumSystemImpl.getPostByID(id);
			String Title = p.getTitle();
			
			editorID = p.getUserID();
			editor = ForumSystemImpl.getUserByID(editorID).getUsername(); 
			
			info.add("post");
			info.add(editor);
			info.add(Title);
		}
		else
			info.add("else");
		
		return info;
	}
	
}
